﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DAL;
using CMSModel;

public partial class Import_cpdb_NTypeLink : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            bd();
            
        }
    }

    void bd()
    {
        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
        var t= dal.NewsType.Where(x => string.IsNullOrEmpty(x.pid));
        foreach (var c in t)
        {
            var tNode = new TreeNode(c.name, c.id);
            AddLocalChild(tNode, dal);
            tvwLocal.Nodes.Add(tNode);
        }
        tvwLocal.ExpandDepth = 0;
        DataTable tb = new StockInfoDAL().GetTypes();
        foreach (DataRow c in tb.Rows)
        {
            var pid = c["ob_parentcode_0031"].ToString();
            if (pid == "02")
            {
                TreeNode tn = new TreeNode(c["ob_sortname_0031"].ToString(), c["ob_sortcode_0031"].ToString());
                AddCpChild(tn, tb);
                tvwCP.Nodes.Add(tn);
            }
        }
    }

    void AddLocalChild(TreeNode fNode,CMSModel.CMSEntities dal)
    {
       var c= dal.NewsType.Where(x => x.enable == 1 && x.pid == fNode.Value);
        
        foreach(var t in c)
        {
            fNode.ChildNodes.Add(new TreeNode(t.name, t.id));
        }
    }

    void AddCpChild(TreeNode fNode,DataTable tb)
    {
        var pid = fNode.Value;
        foreach (DataRow row in tb.Rows)
        {
            if(row["ob_parentcode_0031"].ToString()==pid)
            {
                TreeNode node = new TreeNode(row["ob_sortname_0031"].ToString(), row["ob_sortcode_0031"].ToString());
                fNode.ChildNodes.Add(node);
                AddCpChild(node, tb);
            }
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        TreeNode local = tvwLocal.SelectedNode;
        if (local == null)
        {
            ClientScript.RegisterStartupScript(GetType(), "aa", "alert('请选择本地新闻类型')",true);
            return;
        }
        TreeNode cpNode = tvwCP.SelectedNode;
        if (cpNode == null)
        {
            ClientScript.RegisterStartupScript(GetType(), "aa", "alert('请选择巨潮新闻类型')", true);
            return;
        }
        if (local.ChildNodes.Count > 0)
        {
            ClientScript.RegisterStartupScript(GetType(), "aa", "alert('本地新闻类型还有子类型')", true);
            return;
        }
        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
        if (dal.CPTypeMatching.Any(x => x.LocalTypeId == local.Value && x.CpTypeId == cpNode.Value))
        {
            ClientScript.RegisterStartupScript(GetType(), "aa", "alert('这个类型关联已经存在')", true);
            return;
        }
        CPTypeMatching c = new CPTypeMatching();
        c.CpTypeId = cpNode.Value;
        c.CpTypeName = cpNode.Text;
        c.CTime = DateTime.Now;
        c.LocalTypeId = local.Value;
        dal.CPTypeMatching.AddObject(c);
        dal.SaveChanges();
        ClientScript.RegisterStartupScript(GetType(), "aa", "alert('添加成功');document.location='News.aspx'", true);
    }
}